const db = require("../../config/db");

module.exports = {
    //model数据

    // 评论区数据
    area(news_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_comments where news_id="${news_id}"`;
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "查询失败!",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "查询成功!",
                        data: row,
                    });
                }
            });
        });
    },

    // 详情页评论数据
    common(content, news_id, publish_time, user_id, user_avators, user_nick_name) {
        return new Promise((resolve, reject) => {
            let sql = `insert into db_comments(content,news_id,publish_time,user_id,user_avators,user_nick_name) values("${content}","${news_id}","${publish_time}","${user_id}","${user_avators}","${user_nick_name}")`;
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "评论失败!",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "评论成功!",
                        data: row,
                    });
                }
            });
        });
    },

    // 详情页子评论数据
    commonChild(content, news_id, publish_time, user_id, son_id, user_avators, user_nick_name) {
        return new Promise((resolve, reject) => {
            let sql = `insert into db_comments(content,news_id,publish_time,user_id,son_id,user_avators,user_nick_name) values("${content}","${news_id}","${publish_time}","${user_id}","${son_id}","${user_avators}","${user_nick_name}")`;
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "评论失败!",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "评论成功!",
                        data: row,
                    });
                }
            });
        });
    },

    // 详情页查询内部评论数据
    parent(id, news_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_comments where id="${id}" and news_id="${news_id}"`;
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "未查询到相关评论",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "评论查询成功！",
                        data: row,
                    });
                }
            });
        });
    },

    // 详情页查询子评论数据
    speakList(news_id, son_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_comments where news_id="${news_id}" and son_id="${son_id}"`;
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "未查询到相关评论",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "评论查询成功！",
                        data: row,
                    });
                }
            });
        });
    },
};
